package com.pacy.pacybaseproject.fragments;

import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.baidu.location.BDAbstractLocationListener;
import com.baidu.location.BDLocation;
import com.baidu.location.LocationClient;
import com.baidu.location.LocationClientOption;
import com.baidu.mapapi.map.BaiduMap;
import com.baidu.mapapi.map.BitmapDescriptor;
import com.baidu.mapapi.map.BitmapDescriptorFactory;
import com.baidu.mapapi.map.MapStatus;
import com.baidu.mapapi.map.MapStatusUpdateFactory;
import com.baidu.mapapi.map.MarkerOptions;
import com.baidu.mapapi.map.MyLocationData;
import com.baidu.mapapi.map.OverlayOptions;
import com.baidu.mapapi.model.LatLng;
import com.pacy.pacybaseproject.BR;
import com.pacy.pacybaseproject.R;
import com.pacy.pacybaseproject.base.BaseXFragment;
import com.pacy.pacybaseproject.databinding.CyclingFragmentBinding;
import com.pacy.pacybaseproject.databinding.FragmentBusBinding;
import com.pacy.pacybaseproject.viewmodels.BusViewModel;
import com.pacy.pacybaseproject.viewmodels.CyclingViewModel;

import io.reactivex.annotations.Nullable;
import me.goldze.mvvmhabit.utils.KLog;
import me.goldze.mvvmhabit.utils.MaterialDialogUtils;

/**
 * @author PACY
 * @date 2024/4/15
 * description: 骑行
 */
public class CyclingFragment extends BaseXFragment<CyclingFragmentBinding, CyclingViewModel>  {
    private BaiduMap mBaiduMap;
    private LocationClient mLocationClient;
    public static CyclingFragment newInstance(String title) {

        Bundle args = new Bundle();
        args.putString("title", title);
        CyclingFragment fragment = new CyclingFragment();
        fragment.setArguments(args);
        return fragment;
    }

    @Override
    public int initContentView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        return R.layout.cycling_fragment;
    }

    @Override
    public int initVariableId() {
        return BR.viewModel;
    }

    @Override
    public void initData() {
        super.initData();
        mBaiduMap=binding.mapView.getMap();
        initMapState();
        binding.cycFlee.setChecked(true);

        binding.luxianCsv.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                binding.luxianCsv.setSelectType();
            }
        });
        binding.diancheCsv.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                binding.diancheCsv.setSelectType();
                if(binding.diancheCsv.getSelectType()){
                    binding.dancheCsv.setSelectType(false);
                }
            }
        });
        binding.dancheCsv.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                binding.dancheCsv.setSelectType();
                if(binding.dancheCsv.getSelectType()){
                    binding.diancheCsv.setSelectType(false);
                }
            }
        });
    }

    public void initMapState(){
        MapStatus.Builder builder = new MapStatus.Builder();
        builder.zoom(16.0f);
        mBaiduMap.setMapStatus(MapStatusUpdateFactory.newMapStatus(builder.build()));
        //开起交通图
        mBaiduMap.setTrafficEnabled(false);
//        //打开地图定位图层
        mBaiduMap.setMyLocationEnabled(true);

        //定位初始化
        LocationClient.setAgreePrivacy(true);
        try {
            mLocationClient = new LocationClient(getContext());
            //通过LocationClientOption设置LocationClient相关参数
            LocationClientOption option = new LocationClientOption();
            option.setOpenGps(true); // 打开gps
            option.setCoorType("bd09ll"); // 设置坐标类型
            option.setScanSpan(1000);

//设置locationClientOption
            mLocationClient.setLocOption(option);

//注册LocationListener监听器
            MyLocationListener myLocationListener = new MyLocationListener();
            mLocationClient.registerLocationListener(myLocationListener);
//开启地图定位图层
            mLocationClient.start();
        } catch (Exception e) {
            e.printStackTrace();
            KLog.i(e.toString());
        }
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        binding.mapView.onDestroy();
    }

    @Override
    public void onPause() {
        super.onPause();
        binding.mapView.onPause();
    }

    @Override
    public void onResume() {
        super.onResume();
        binding.mapView.onResume();
    }

    @Override
    public void onViewStateRestored(@Nullable Bundle savedInstanceState) {
        super.onViewStateRestored(savedInstanceState);
        binding.mapView.onSaveInstanceState(savedInstanceState);
    }

    public class MyLocationListener extends BDAbstractLocationListener {
        @Override
        public void onReceiveLocation(BDLocation location) {
            //mapView 销毁后不在处理新接收的位置
            if (location == null || binding.mapView == null){
                return;
            }
            MyLocationData locData = new MyLocationData.Builder()
                    .accuracy(location.getRadius())
                    // 此处设置开发者获取到的方向信息，顺时针0-360
                    .direction(location.getDirection()).latitude(location.getLatitude())
                    .longitude(location.getLongitude()).build();
            mBaiduMap.setMyLocationData(locData);
            mBaiduMap.animateMapStatus(MapStatusUpdateFactory.newLatLng(new LatLng(location.getLatitude(),location.getLongitude())));
        }
    }
}
